<template>
  <el-dialog
    :title="!dataForm.empno ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="员工姓名" prop="ename">
      <el-input v-model="dataForm.ename" placeholder="员工姓名"></el-input>
    </el-form-item>
    <el-form-item label="员工职务" prop="job">
      <el-input v-model="dataForm.job" placeholder="员工职务"></el-input>
    </el-form-item>
    <el-form-item label="薪资" prop="sal">
      <el-input 
      type="number"
      min="0"
      v-model="dataForm.sal" placeholder="薪资"></el-input>
    </el-form-item>
    <el-form-item label="入职日期" prop="hiredate">
    <el-col :span="11">
      <el-date-picker type="date" placeholder="请选择选择日期" v-model="dataForm.hiredate" style="width: 100%;"></el-date-picker>
    </el-col>
  </el-form-item>

    <el-form-item label="奖金" prop="comm">
      <el-input
      type="number"
      min="0"
       v-model="dataForm.comm" placeholder="奖金"></el-input>
    </el-form-item>
    
    <el-form-item label="部门编号" prop="deptno">
    <el-select v-model="dataForm.deptno" 
    @focus="getDeptList"
    placeholder="请选择部门编号">
       <el-option
      v-for="item in deptList"
      :key="item.deptname"
      :label="item.deptname"
      :value="item.deptno">
    </el-option>
    </el-select>
  </el-form-item>
    <el-form-item label="城市" prop="location">
    <el-select v-model="dataForm.location" 
    @focus="getCityList"
    placeholder="请选择城市">
       <el-option
      v-for="item in cityList"
      :key="item.cityName"
      :label="item.cityName"
      :value="item.cityId">
    </el-option>
    </el-select>
  </el-form-item>
    <el-form-item label="上级" prop="managerId">
      <el-input v-model="dataForm.managerId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input 
      type="number"
      min="18"
      v-model="dataForm.age" placeholder=""></el-input>
    </el-form-item>  
    <el-form-item label="性别" prop="gender">
    <el-radio-group v-model="dataForm.gender">
      <el-radio label="男"></el-radio>
      <el-radio label="女"></el-radio>
    </el-radio-group>
  </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        deptList: [],
        cityList: [],
        dataForm: {
          empno: 0,
          ename: '',
          job: '',
          sal: '',
          hiredate: '',
          comm: '',
          deptno: '',
          location: '',
          managerId: '',
          age: '',
          gender: ''
        },
        dataRule: {
          ename: [
            { required: true, message: '员工姓名不能为空', trigger: 'blur' }
          ],
          job: [
            { required: true, message: '员工职务不能为空', trigger: 'blur' }
          ],
          sal: [
            { required: true, message: '薪资不能为空', trigger: 'blur' }
          ],
          hiredate: [
            { required: true, message: '入职日期不能为空', trigger: 'blur' }
          ],
          comm: [
            { required: true, message: '奖金不能为空', trigger: 'blur' }
          ],
          deptno: [
            { required: true, message: '部门编号不能为空', trigger: 'blur' }
          ],
          location: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          managerId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.empno = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.empno) {
            this.$http({
              url: this.$http.adornUrl(`/emp/info/${this.dataForm.empno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.ename = data.emp.ename
                this.dataForm.job = data.emp.job
                this.dataForm.sal = data.emp.sal
                this.dataForm.hiredate = data.emp.hiredate
                this.dataForm.comm = data.emp.comm
                this.dataForm.deptno = data.emp.deptno
                this.dataForm.location = data.emp.location
                this.dataForm.managerId = data.emp.managerId
                this.dataForm.age = data.emp.age
                this.dataForm.gender = data.emp.gender
              }
            })
          }
        })
      },
      // 获取部门列表
      getDeptList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/dept/list'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data.page.list)
            this.deptList = data.page.list
          }
          this.dataListLoading = false
        })
      },
      // 获取城市列表
      getCityList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/locations/list'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data.page.list)
            this.cityList = data.page.list
          }
          this.dataListLoading = false
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/emp/${!this.dataForm.empno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'empno': this.dataForm.empno || undefined,
                'ename': this.dataForm.ename,
                'job': this.dataForm.job,
                'sal': this.dataForm.sal,
                'hiredate': this.dataForm.hiredate,
                'comm': this.dataForm.comm,
                'deptno': this.dataForm.deptno,
                'location': this.dataForm.location,
                'managerId': this.dataForm.managerId,
                'age': this.dataForm.age,
                'gender': this.dataForm.gender
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
